package com.pdg.mcplugin.tsa;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.bukkit.configuration.MemorySection;

import com.pdg.mcplugin.common.baseclasses.PluginClientBase;

public class ContrabandManager extends PluginClientBase<TSA>{
	private static final String ENABLED_KEY = "contraband.enabled";
	private static final String DEFAULT_LIST_KEY = "contraband.defaultList";
	private boolean enabled;
	private String defaultList;
	private Map<String,ContrabandList> contrabandLists = new HashMap<String,ContrabandList>();

	public ContrabandManager(TSA thePlugin) {
		super(thePlugin);
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled,boolean commit) {
		this.enabled = enabled;
		if(commit) commit();
	}

	public void commit() {
		MemorySection theConfig = getPlugin().getConfig();
		theConfig.set(ENABLED_KEY, isEnabled());
		theConfig.set(DEFAULT_LIST_KEY, getDefaultList());
		Iterator<ContrabandList> iterator = contrabandLists.values().iterator();
		while(iterator.hasNext()){
			ContrabandList theList = iterator.next();
			theList.commit();
		}
	}

	public void setEnabled(boolean enabled) {
		setEnabled(enabled,true);
	}

	public String getDefaultList() {
		return defaultList;
	}

	public void setDefaultList(String defaultList,boolean commit) {
		this.defaultList = defaultList;
		if(commit) commit();
	}

	public void setDefaultList(String defaultList) {
		setDefaultList(defaultList,true);
	}

	public ContrabandList getContrabandList(String theKey) {
		if(contrabandLists.containsKey(theKey)){
			return contrabandLists.get(theKey);
		}else{
			ContrabandList theList = new ContrabandList(this);
			contrabandLists.put(theKey, theList);
			return theList;
		}
	}

	public String findContrabandList(ContrabandList contrabandList) {
		Iterator<Entry<String,ContrabandList>> iterator = contrabandLists.entrySet().iterator();
		while(iterator.hasNext()){
			Entry<String,ContrabandList> entry = iterator.next();
			if(entry.getValue()==contrabandList){
				return entry.getKey();
			}
		}
		return null;
	}

}
